﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowledgeAssistant
{
    public partial class RolesForm : Form
    {
        public RolesForm()
        {
            InitializeComponent();
            RefreshData();
        }

        private void RefreshData()
        {
            permissionscombobox.DataSource = RoleUtil.GetPermissions();
            permissionscombobox.DisplayMember = "Description";
            if (permissionscombobox.Items.Count != 0)
            {
                permissionscombobox.SelectedIndex = 0;
                permissionscombobox.Text = permissionscombobox.Items[permissionscombobox.SelectedIndex].ToString();
            }

            roleslistbox.DataSource = RoleUtil.getListRoles();
            roleslistbox.DisplayMember = "Name";

            if (roleslistbox.Items.Count != 0)
            {
                roleslistbox.SelectedIndex = 0;
                Role r = (Role)roleslistbox.Items[0];
                permissionslistbox.DataSource = r.Permissions;
                permissionslistbox.DisplayMember = "Description";

                if (permissionslistbox.Items.Count != 0)
                {
                    permissionslistbox.SelectedIndex = 0;
                }
            }
        }

        private void editrole_Click(object sender, EventArgs e)
        {
            EditRole er = new EditRole((Role)this.roleslistbox.SelectedItem, EditRole.ActionType.edit);
            er.ShowDialog();
            RefreshRoles();
        }

        private void addrole_Click(object sender, EventArgs e)
        {
            EditRole er = new EditRole(EditRole.ActionType.add);
            er.ShowDialog();
            RefreshRoles();
        }

        private void RefreshRoles()
        {
            roleslistbox.DataSource = RoleUtil.getListRoles();
            roleslistbox.DisplayMember = "Name";
        }

        private void roleslistbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (roleslistbox.SelectedIndex != -1)
            {
                Role r = (Role)roleslistbox.SelectedItem;
                permissionslistbox.DataSource = r.Permissions;
                permissionslistbox.DisplayMember = "Description";
                if (permissionslistbox.Items.Count != 0)
                {
                    permissionslistbox.SelectedIndex = 0;
                }
                DisableControlsForThreeBasedRole();
            }
        }

        private void DisableControlsForThreeBasedRole()
        {
            if (((Role)roleslistbox.SelectedValue).Name == "Admin")
            {
                
                deletepermission.Enabled = false;
                addpermission.Enabled = false;
                editrole.Enabled = false;
                permissionscombobox.Enabled = false;
                removerole.Enabled = false;
            }
            else
            {
                if ((((Role)roleslistbox.SelectedValue).Name == "Student") ||
                (((Role)roleslistbox.SelectedValue).Name == "Teacher"))
                {

                    deletepermission.Enabled = false;
                    addpermission.Enabled = false;
                    editrole.Enabled = true;
                    permissionscombobox.Enabled = true;
                    removerole.Enabled = false;
                }
                else
                {
                    permissionscombobox.Enabled = true; ;

                    deletepermission.Enabled = true;
                    addpermission.Enabled = true;
                    editrole.Enabled = true;
                    removerole.Enabled = true;
                }
            }
        }
           

        private void deletepermission_Click(object sender, EventArgs e)
        {
            Role r = (Role)roleslistbox.SelectedItem;
            Permission p = (Permission)permissionslistbox.SelectedItem;
            if (roleslistbox.SelectedItem != null && permissionslistbox.SelectedItem != null)
            {
                RoleUtil.DeletePermission(r.RoleID, p.ID);
                r.Permissions = RoleUtil.GetPermissions(r.RoleID);
                RefreshRolePermissions();
            }
        }

        private void RefreshRolePermissions()
        {
            Role r = (Role)roleslistbox.SelectedItem;
            permissionslistbox.DataSource = r.Permissions;
            permissionslistbox.DisplayMember = "Description";
        }

        private void addpermission_Click(object sender, EventArgs e)
        {
            if (permissionscombobox.SelectedItem != null && !IsContainsPermission((Permission)permissionscombobox.SelectedItem))
            {
                Role r = (Role)roleslistbox.SelectedItem;
                RoleUtil.AddPermission(
                    r.RoleID,
                    ((Permission)permissionscombobox.SelectedItem).ID
                    );
                r.Permissions = RoleUtil.GetPermissions(r.RoleID);
                RefreshRolePermissions();
            }
        }

        private bool IsContainsPermission(Permission p)
        {
            foreach (Permission per in (List<Permission>)(permissionslistbox.DataSource))
            {
                if (per.ID == p.ID) { return true; }
            }
            return false;
        }

        private void removerole_Click(object sender, EventArgs e)
        {
            if (roleslistbox.SelectedItem != null && !RoleUtil.IsRoleIsUsedByUser(((Role)roleslistbox.SelectedItem).RoleID))
            {
                RoleUtil.DeleteRole(((Role)roleslistbox.SelectedItem).RoleID);
                RefreshRoles();
            }
        }

        private void close_RolesForm_button_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
